package org.grayrabbit.cloud.utils;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.grayrabbit.cloud.core.Constants;


public class CommandBuilder {
		
	public static String commCommand(String cmd,Property[] pro){
		try{
			return PropertyBuilder.xmlProperties(cmd,pro);
		}
		catch(ParserConfigurationException pe){	
			pe.printStackTrace();
		}
		catch(TransformerException te){		
			te.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String LoginCommand(String uid,String pwd){		
				
		String suid = "web";
		String spwd = "web";
		
		if(uid!=null && pwd!=null){
			suid = uid;
			spwd = pwd;
		}
		
		Property Pro[] = new Property[2];			
		Pro[0] = new Property("uid",suid);
		Pro[1] = new Property("pwd",spwd);
		return commCommand(Constants.MODULE_COMMAND_SESSION_LOGIN,Pro);
			
	}
	
	public static String TickCommand(String info){			
		
		Property Pro[] = new Property[1];
		Pro[0] = new Property("info",info);
		return commCommand(Constants.MODULE_COMMAND_SYSTEM_TICK,Pro);			
	}
	
	public static String SystemCommand(String cmd,String module){			
		
		if(module==null){
			Property Pro[] = new Property[1];
			Pro[0] = new Property("cmd",cmd);
			return commCommand(Constants.MODULE_COMMAND_SYSTEM_SYSTEM,Pro);
		}else{
			Property Pro[] = new Property[2];
			Pro[0] = new Property("cmd",cmd);
			Pro[1] = new Property("module",module);
			return commCommand(Constants.MODULE_COMMAND_SYSTEM_SYSTEM,Pro);
		}
	}
	
	public static String RegisterCommand(String name,String url){
		
		Property Pro[] = new Property[2];			
		Pro[0] = new Property("name",name);
		Pro[1] = new Property("url",url);
		return commCommand(Constants.MODULE_COMMAND_MODULE_REGISTER,Pro);
		
	}
	
	public static String ProcedureCommand(String conn,String name){		
		
		if(name!=null && conn!=null){
			
			Property Pro[] = new Property[2];
			Pro[0] = new Property("conn",conn);
			Pro[1] = new Property("name",name);			
			return commCommand(Constants.MODULE_COMMAND_DATA_PROCEDURE,Pro);			
		}
		
		return null;
	}
	
	public static String StatementCommand(String conn,String sql,String type){		
		
		if(type!=null && sql!=null && conn!=null){
			
			Property Pro[] = new Property[3];
			Pro[0] = new Property("conn",conn);			
			Pro[1] = new Property("sql",sql);
			Pro[2] = new Property("type",type);
			return commCommand(Constants.MODULE_COMMAND_DATA_STATEMENT,Pro);			
		}		
		return null;
	}	
	
	public static String BusinessCommand(String name){		
		
		if(name!=null){
			
			Property Pro[] = new Property[1];			
			Pro[0] = new Property("name",name);			
			return commCommand(Constants.MODULE_COMMAND_BIZ_BUSINESS,Pro);			
		}		
		return null;
	}	
	
	public static String ScriptCommand(String file,String method){		
		
		if(file!=null){
			
			Property Pro[] = new Property[2];
			Pro[0] = new Property("file",file);
			Pro[1] = new Property("method",method);			
			return commCommand(Constants.MODULE_COMMAND_BIZ_SCRIPT,Pro);			
		}
		
		return null;
	}
	
	public static String SpringCommand(String className,String method,String id){		
		
		if(className!=null && method!=null){
			
			if(id==null){
				Property Pro[] = new Property[2];
				Pro[0] = new Property("class",className);
				Pro[1] = new Property("method",method);					
				return commCommand(Constants.MODULE_COMMAND_BIZ_SPRING,Pro);
			}
			else{
				Property Pro[] = new Property[3];
				Pro[0] = new Property("class",className);
				Pro[1] = new Property("method",method);	
				Pro[2] = new Property("id",id);					
				return commCommand(Constants.MODULE_COMMAND_BIZ_SPRING,Pro);
			}
		}
		
		return null;
	}	
	
	public static String ProxyCommand(String className,String method){		
		
		if(className!=null && method!=null){
			
			Property Pro[] = new Property[2];
			Pro[0] = new Property("class",className);
			Pro[1] = new Property("method",method);					
			return commCommand(Constants.MODULE_COMMAND_BIZ_SPRING,Pro);
		}
		
		return null;
	}	
	
	public static String BankCommand(String gwcode,String bank,String code,String request){		
		
		if(gwcode!=null && bank!=null && code!=null){
			
			Property Pro[] = new Property[4];
			Pro[0] = new Property("gwcode",gwcode);
			Pro[1] = new Property("bank",bank);	
			Pro[2] = new Property("code",code);
			Pro[3] = new Property("request",request);	
			return commCommand(Constants.MODULE_COMMAND_BIZ_BANK,Pro);
		}
		
		return null;
	}	
	
	
	public static void main(String[] arc){
		
		System.out.println(BankCommand("com.iii","test","com.iii","test"));
		
	}
	
}
